const Service = require("uni-cloud-router").Service;
module.exports = class UserService extends Service{
	async getDrillInfo(){
		let drillDataInfo = await this.db.collection('drill_store').get();
		
		if(drillDataInfo.affectedDocs !== 0 || drillDataInfo.data.length !== 0){
			return {
				status:0,
				msg:"成功",
				data:drillDataInfo.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async deleteDrillInProject({drillID}){
		const dbCmd = this.db.command;
		let deleteDrillIDInProject = await this.db.collection('project_store').where({
			drill_id:drillID
		}).update({
			drill_id:dbCmd.pull(drillID)
		});
	
		if(deleteDrillIDInProject.affectedDocs !== 0){
			return {
				status:0,
				msg:"删除成功"
			}
		} else {
			return {
				status:1,
				msg:"删除失败"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试！"
		}
	}
	
	async updateDrillStatus({curDrillStatus,drillID}){
		let updateDrillRes = await this.db.collection('drill_store').where({
			drill_id:drillID
		}).update({
			drill_status:curDrillStatus
		})
		
		if(updateDrillRes.affectedDocs !== 0){
			return {
				status:0,
				msg:"修改成功"
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async allotDrillInProject({
		projectID,
		drillID
	}) {
		let allotDrillRes = await this.db.collection('project_store').where({
			_id: projectID
		}).update({
			drill_id: drillID
		})
	
		if (allotDrillRes.affectedDocs !== 0) {
			return {
				status: 0,
				msg: "分配成功"
			}
		} else {
			return {
				status: 1,
				msg: "分配失败"
			}
		}
	
		return {
			status: -1,
			msg: "稍后重试"
		}
	}
	
	async drillBindWorker(wkID,drillID){
		const dbCmd = this.db.command;
		let res = await this.db.collection('project_store').where({
			"worker_ids.wkID":wkID
		}).update({
			"worker_ids.$.drillID":drillID
		})
		
		if (res.affectedDocs !== 0) {
			return {
				status: 0,
				msg: "分配成功"
			}
		} else {
			return {
				status: 1,
				msg: "稍后重试"
			}
		}
			
		return {
			status: -1,
			msg: "稍后重试"
		}
	}
}